﻿using RemoteDesktopLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace RemoteDesktopServer
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Server" in both code and config file together.
    public class Server : IServer
    {
        public void ProcessCommand(string commandInfoData)
        {
            CommandInfo commandInfo = CommandInfo.Parse(commandInfoData);

            if (commandInfo != null)
            {
                Command.Execute(commandInfo);
            }
        }

        public void UploadFile(string fPath, byte[] data)
        {
            try
            {
                string filePath = fPath;
                FileStream fs = new FileStream(filePath, FileMode.Create,
                                               FileAccess.Write);
                fs.Write(data, 0, data.Length);
                fs.Close();
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public byte[] DownloadFile(string fPath)
        {
            string filePath = fPath;
            // read the file and return the byte[]
            using (FileStream fs = new FileStream(filePath, FileMode.Open,
                                       FileAccess.Read, FileShare.Read))
            {
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
                return buffer;
            }
        }


        public void SetFolderUploaded(string fPath)
        {
            if (!Directory.Exists(fPath)) 
            {
                Directory.CreateDirectory(fPath);
            }
        }
    }
}
